<?php
namespace app\index\controller;
use app\model\User as UserModel;
use think\Db;


class Index extends Base
{
 


    public function index()
    {
        $ulogin = $this->login_user;

        $page_info = $this->page_info;
        $page_info['title'] = '首页';

        $categorys = Db::name('category')->select();
        $assistants = Db::name('assistant')->select();

        $sessions = Db::name('session')->where('user_id',$ulogin['id'])->order('id','desc')->select();
        foreach ($sessions as $k => $vo) {
            $assistant = Db::name('assistant')->where('id',$vo['assistant_id'])->find();
            $sessions[$k]['assistant'] = $assistant;

            $msg = Db::name('msg')->where('session_sn',$vo['session_sn'])->order('id','desc')->find();
            if ( !empty($msg['reply']) ) {
                if ($msg['type'] == 'text') {
                    $sessions[$k]['msg'] = [
                        'message'=>$msg['reply'],
                        'ctime'=>date('y-m-d H:i',$msg['rtime'])
                    ];
                }elseif($msg['type'] == 'htsc'){
                    $message = json_decode($msg['message'],true);
                    $sessions[$k]['msg'] = [
                        'message'=>'关于《'.$message['name'].'》的审查结果',
                        'ctime'=>date('y-m-d H:i',$msg['rtime'])
                    ];
                }
            }else{
                $sessions[$k]['msg'] = [
                    'message'=>$assistant['description'],
                    'ctime'=>date('y-m-d H:i',$vo['ctime'])
                ];
            }
            
        }


        return view('index', [
            'categorys'=>$categorys,
            'assistants'=>$assistants,
            'sessions'=>$sessions
        ]);
    }

    public function prompt($msg=''){
        return view('prompt', [
            'msg'=>$msg,
        ]);
    }

    public function help(){
        $page_info = $this->page_info;
        $page_info['title'] = '帮助中心';

        return view('help', [
            'page_info'=>$page_info,
        ]);
    }

    public function dashboard(){
        // $login_url = 'https://home4service.console.aliyun.com/home/dashboard/ProductAndService';
        // $login_url = 'https://home.console.aliyun.com/home/dashboard/ProductAndService';
        $login_res = $this->dashboardLogin();
        return redirect($login_res);
    }

    // 备案
    public function beian(){
        $login_url = 'https://beian4service.aliyun.com';
        $login_res = $this->Login($login_url);
        return redirect($login_res);
    }

    // 续费管理
    public function renew(){
        $login_url = 'https://usercenter24service.aliyun.com/renew/manual?hideSideBar=true&hideTopBar=true';
        $login_res = $this->Login($login_url);
        return redirect($login_res);
    }

    // 退订管理
    public function refund(){
        $login_url = 'https://usercenter24service.aliyun.com/refund/refund?hideSideBar=true&hideTopBar=true';
        $login_res = $this->Login($login_url);
        return redirect($login_res);
    }

    // 订单管理
    public function order(){
        $login_url = 'https://usercenter24service.aliyun.com/order/list?hideSideBar=true&hideTopBar=true';
        $login_res = $this->Login($login_url);
        return redirect($login_res);
    }

    // 财务总览
    public function overview(){
        $login_url = 'https://usercenter24service.aliyun.com/finance/expense-report/overview?hideSideBar=true&hideTopBar=true';
        $login_res = $this->Login($login_url);
        return redirect($login_res);
    }

    // 资源实例管理
    public function summary(){
        $login_url = 'https://usercenter24service.aliyun.com/ri/summary?hideSideBar=true&hideTopBar=true?commodityCode=';
        $login_res = $this->Login($login_url);
        return redirect($login_res);
    }

}
